<?php

require_once 'vendor/autoload.php';

use Smalot\Cups\Builder\Builder;
use Smalot\Cups\Manager\JobManager;
use Smalot\Cups\Model\Job;
use Smalot\Cups\Transport\Client;
use Smalot\Cups\Transport\ResponseParser;
use Http\Client\Socket\Client as HttpClient;

/**
 * 立即执行打印测试 - EPSON L4260 Series
 * 打印机IP: 10.1.0.12
 * 
 * 此脚本将立即发送打印任务到打印机
 */

echo "🖨️  EPSON L4260 Series 立即打印测试\n";
echo "打印机IP: 10.1.0.12\n";
echo str_repeat('=', 50) . "\n\n";

// 确认打印
echo "⚠️  此脚本将立即发送打印任务到打印机！\n";
echo "请确保：\n";
echo "✓ 打印机已开机并连接到网络\n";
echo "✓ 打印机有足够的纸张\n";
echo "✓ 打印机墨水/碳粉充足\n";
echo "✓ 您确实想要执行打印测试\n\n";

echo "按 'y' 继续打印，按任意其他键取消: ";
$input = trim(fgets(STDIN));

if (strtolower($input) !== 'y') {
    echo "\n❌ 打印已取消。\n";
    exit(0);
}

echo "\n🚀 开始执行打印...\n\n";

try {
    // 1. 初始化组件
    echo "1. 初始化CUPS-IPP组件...\n";
    $builder = new Builder();
    $client = new Client();
    $httpClient = new HttpClient();
    $responseParser = new ResponseParser();
    $jobManager = new JobManager($builder, $httpClient, $responseParser);
    echo "✓ 组件初始化完成\n\n";
    
    // 2. 创建打印机对象
    echo "2. 创建打印机对象...\n";
    $printer = new \Smalot\Cups\Model\Printer();
    $printer->setUri('ipp://10.1.0.12:631/ipp/print');
    $printer->setName('EPSON L4260 Series');
    echo "✓ 打印机对象创建完成\n";
    echo "  - 打印机URI: " . $printer->getUri() . "\n";
    echo "  - 打印机名称: " . $printer->getName() . "\n\n";
    
    // 3. 创建打印任务
    echo "3. 创建打印任务...\n";
    $job = new Job();
    $job->setName('EPSON L4260 实际打印测试');
    $job->setUsername('cups-ipp-test');
    $job->setCopies(1);
    $job->setSides('one-sided');
    
    // 创建打印内容
    $printContent = createPrintContent();
    $job->addText($printContent);
    
    echo "✓ 打印任务创建完成\n";
    echo "  - 任务名称: " . $job->getName() . "\n";
    echo "  - 用户名: " . $job->getUsername() . "\n";
    echo "  - 份数: " . $job->getCopies() . "\n\n";
    
    // 4. 发送打印任务
    echo "4. 发送打印任务到打印机...\n";
    echo "正在连接到 10.1.0.12:631...\n";
    
    $result = $jobManager->send($printer, $job);
    
    if ($result) {
        echo "\n🎉 打印任务发送成功！\n";
        echo "📄 任务详情:\n";
        echo "  - 任务ID: " . $result->getId() . "\n";
        echo "  - 状态: " . $result->getState() . "\n";
        echo "  - 打印机: EPSON L4260 Series\n";
        echo "  - 发送时间: " . date('Y-m-d H:i:s') . "\n";
        
        echo "\n✅ 请检查打印机，测试页应该正在打印...\n";
        
    } else {
        echo "\n❌ 打印任务发送失败\n";
        echo "可能的原因：\n";
        echo "- 打印机未开机或网络连接问题\n";
        echo "- IPP服务未启用\n";
        echo "- 防火墙阻止连接\n";
    }
    
} catch (Exception $e) {
    echo "\n❌ 打印执行失败\n";
    echo "错误信息: " . $e->getMessage() . "\n";
    echo "错误文件: " . $e->getFile() . ":" . $e->getLine() . "\n\n";
    
    echo "🔧 故障排除建议:\n";
    echo "1. 检查打印机是否开机并连接网络\n";
    echo "2. 验证IP地址 10.1.0.12 是否正确\n";
    echo "3. 检查防火墙设置\n";
    echo "4. 确认打印机支持IPP协议\n";
    echo "5. 尝试从其他应用程序打印以验证打印机状态\n";
    
    // 提供备用方案
    echo "\n💡 备用方案 - Windows系统打印:\n";
    tryWindowsPrint();
}

echo "\n" . str_repeat('=', 50) . "\n";
echo "测试完成！\n";

/**
 * 创建打印内容
 */
function createPrintContent() {
    $content = "";
    $content .= str_repeat('=', 60) . "\n";
    $content .= "           EPSON L4260 Series 网络打印测试\n";
    $content .= str_repeat('=', 60) . "\n\n";
    
    $content .= "测试信息:\n";
    $content .= "- 测试时间: " . date('Y-m-d H:i:s') . "\n";
    $content .= "- 打印机IP: 10.1.0.12\n";
    $content .= "- 协议: Internet Printing Protocol (IPP)\n";
    $content .= "- 端口: 631\n";
    $content .= "- 库: CUPS-IPP PHP Library\n";
    $content .= "- PHP版本: " . PHP_VERSION . "\n\n";
    
    $content .= "网络连接测试:\n";
    $content .= "✓ Ping测试通过\n";
    $content .= "✓ IPP端口631连接成功\n";
    $content .= "✓ 路由追踪正常 (1跳直达)\n";
    $content .= "✓ 打印机识别为: EPSONBD5110\n\n";
    
    $content .= "功能验证:\n";
    $content .= "✓ 网络通信\n";
    $content .= "✓ IPP协议\n";
    $content .= "✓ 任务创建\n";
    $content .= "✓ 任务发送\n";
    $content .= "✓ 文本打印\n";
    $content .= "✓ 中文支持: 爱普生网络打印机测试\n";
    $content .= "✓ 特殊字符: @#$%^&*()_+-=[]{}|\n\n";
    
    $content .= "字符测试:\n";
    $content .= "大写字母: ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
    $content .= "小写字母: abcdefghijklmnopqrstuvwxyz\n";
    $content .= "数字测试: 0123456789\n";
    $content .= "标点符号: .,;:!?\"'()[]{}\n\n";
    
    $content .= "打印质量检查:\n";
    $content .= "请检查以下内容是否清晰可读：\n";
    $content .= "- 所有文字是否清晰\n";
    $content .= "- 字符间距是否正常\n";
    $content .= "- 行间距是否合适\n";
    $content .= "- 页面边距是否正确\n\n";
    
    $content .= str_repeat('-', 60) . "\n";
    $content .= "如果您能清晰地看到这个完整的测试页面，\n";
    $content .= "说明EPSON L4260网络打印功能完全正常！\n";
    $content .= str_repeat('-', 60) . "\n\n";
    
    $content .= "技术支持信息:\n";
    $content .= "- 测试脚本: print_now.php\n";
    $content .= "- 库版本: CUPS-IPP Latest\n";
    $content .= "- 系统: " . PHP_OS . "\n";
    $content .= "- 时间戳: " . time() . "\n";
    
    return $content;
}

/**
 * 尝试Windows系统打印作为备用方案
 */
function tryWindowsPrint() {
    if (PHP_OS_FAMILY !== 'Windows') {
        return;
    }
    
    echo "尝试Windows系统打印...\n";
    
    // 创建简单的测试文件
    $testFile = 'backup_print_test.txt';
    $content = "EPSON L4260 Series 备用打印测试\n" .
              "时间: " . date('Y-m-d H:i:s') . "\n" .
              "IP: 10.1.0.12\n" .
              "如果看到此页面，说明Windows系统打印正常。";
    
    file_put_contents($testFile, $content);
    
    // 尝试使用Windows打印命令
    $printerName = 'EPSON L4260 Series';
    $command = "print /D:\"$printerName\" \"$testFile\"";
    
    echo "执行命令: $command\n";
    
    $output = [];
    $returnCode = 0;
    exec($command . ' 2>&1', $output, $returnCode);
    
    if ($returnCode === 0) {
        echo "✓ Windows系统打印命令执行成功\n";
    } else {
        echo "❌ Windows系统打印也失败\n";
        echo "输出: " . implode('\n', $output) . "\n";
    }
    
    // 清理测试文件
    if (file_exists($testFile)) {
        unlink($testFile);
    }
}